/*
 *             Copyright (C) 2024 Steve Jrong
 *
 * 	   GitHub Homepage: https://www.github.com/SteveJrong
 *      Gitee Homepage: https://gitee.com/stevejrong1024
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.stevejrong.waiter.mq.message.component.test.config.spring;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;

/**
 * 测试配置类 - RocketMQ生产者配置
 * <p>
 * RocketMQ单机模式配置
 *
 * @author Steve Jrong
 * @since 1.0
 */
@Configuration
@ConditionalOnResource(resources = "classpath:application-dev.yml")
@PropertySource(value = "classpath:application-dev.yml", factory = TestYamlPropertySourceFactory.class)
@Data
@RequiredArgsConstructor
public class MqProducerConfig {

	@Value("${waiter.mq-message-component.rocketmq.name-server}")
	private String nameServer;

	@Value("${waiter.mq-message-component.rocketmq.producer.group}")
	private String group;

	@Bean
	public DefaultMQProducer mqProducer() throws MQClientException {
		DefaultMQProducer mqProducer = new DefaultMQProducer(group); // 创建消息生产者
		mqProducer.setNamesrvAddr(nameServer); // NameServer的地址
		mqProducer.start(); // 启动生产者组
		return mqProducer;
	}
}